package com.huc.other.lambda;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class LambdaDemo {
    public static void main(String[] args) {
        //mapForEach();
        //listForEach();
        createThread();
    }

    //循环遍历map获取所有结果
    private static void mapForEach() {
        Map<String, String> map = new HashMap<>();
        map.put("A", "a");
        map.put("B", "b");
        map.put("C", "c");
        map.put("D", "d");

        System.out.println("map普通方式遍历:");
        for (String key : map.keySet()) {
            System.out.println(String.format("k=%s,v=%s", key, map.get(key)));
        }
        System.out.println("map拉姆达表达式遍历:");
        map.forEach((k, v) -> {
            System.out.println(String.format("k=%s,v=%s", k, v));
        });
    }

    //循环遍历list获取所有结果
    private static void listForEach() {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("22");
        list.add("333");
        list.add("4444");
        System.out.println("list普通方式遍历:");
        for (String item : list) {
            System.out.println(item);
        }
        System.out.println("list拉姆达表达式遍历:");
        list.forEach((item) -> {
            System.out.println(item);
        });
        System.out.println("list双冒号运算符遍历:");
        list.forEach(System.out::println);
    }

    //创建线程
    private static void createThread() {
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("创建线程运行");
            }
        };
        r1.run();//特殊用法
        Runnable r2 = () -> System.out.println("拉姆达方式创建线程!");
        r2.run();//特殊用法
    }


}
